package example.JAXB.JAXBexample;

import java.io.StringWriter;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

public class MarshllerSample {
	public String getJAXBString() throws JAXBException{
		// =============================================================================================================
        // Setup JAXB
        // =============================================================================================================
 
        // Create a JAXB context passing in the class of the object we want to marshal/unmarshal
        final JAXBContext context = JAXBContext.newInstance(SampleObject.class);
 
        // =============================================================================================================
        // Marshalling OBJECT to XML
        // =============================================================================================================
 
        // Create the marshaller, this is the nifty little thing that will actually transform the object into XML
        final Marshaller marshaller = context.createMarshaller();
 
        // Create a stringWriter to hold the XML
        final StringWriter stringWriter = new StringWriter();
        
        SampleObject sampleObject = new SampleObject();
        
        //Marshal the javaObject and write the XML to the stringWriter
        marshaller.marshal(sampleObject, stringWriter);
        
        String result = stringWriter.toString();
        
        return result;
	}
}
